﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cpe.cpein.DATA;

namespace cpe.cpein.BUSINESS
{
    public static class BUGroup
    {
        /// <summary>
        /// Get All the Group of the database.
        /// </summary>
        /// <param name="searchInDeleted"></param>
        /// <returns></returns>
        public static List<Group> GetAllGroup(bool searchInDeleted)
        {
            List<Group> Groups = new List<Group>();
            using (cpeinEntities db = new cpeinEntities())
            {
                var results = from Group in db.Group
                              where Group.grp_isdeleted == searchInDeleted
                              select Group;
                foreach (Group tmp in results.ToList())
                    Groups.Add(tmp);
            }
            return Groups;
        }

        /// <summary>
        /// Function used to get all members of a group.
        /// </summary>
        /// <param name="grp"></param>
        /// <returns></returns>
        public static List<User> GetGroupMembers(Group grp)
        {   
            List<User> Users = new List<User>();
            if (grp != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var results = from User in db.User
                                  join Utg in db.UserToGroup on User.usr_id equals Utg.usr_id
                                  where User.usr_isdeleted == false
                                     && Utg.utg_isdeleted == false
                                     && Utg.grp_id == grp.grp_id
                                  select User;
                    Users.AddRange(results.ToList());
                }
            }
            return Users;
        }

        /// <summary>
        /// Get the Group by id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Group GetGroupByID(int id)
        {
            if (id <= 0)
                return null;
            else
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var grp = from Group in db.Group
                              where Group.grp_id == id
                              select Group;
                    foreach (Group tmp in grp)
                        return tmp;
                    return null;
                }
            }
        }

        /// <summary>
        /// grp a Group into the database.
        /// </summary>
        /// <param name="grp"></param>
        /// <returns></returns>
        public static Group AddGroup(Group grp)
        {
            if (grp != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    Group ngrp = db.Group.Add(grp);
                    db.SaveChanges();
                    return ngrp;
                }
            }
            else
                return null;
        }

        /// <summary>
        /// Remove a specific Group of the database.
        /// </summary>
        /// <param name="grp"></param>
        /// <returns></returns>
        public static bool RemoveGroup(Group grp)
        {
            if (grp != null)
            {
                grp.grp_isdeleted = true;
                if (UpdateGroup(grp) != null)
                {
                    using (cpeinEntities db = new cpeinEntities())
                    {
                        db.SaveChanges();
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Update the Group into the database.
        /// </summary>
        /// <param name="grp"></param>
        /// <returns></returns>
        public static Group UpdateGroup(Group grp)
        {
            if (grp != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var current = from Group in db.Group
                                  where Group.grp_id == grp.grp_id
                                  select Group;

                    foreach (var tmp in current.ToList())
                    {
                        tmp.grp_label = grp.grp_label;
                        tmp.grp_type = grp.grp_type;
                        tmp.grp_description = grp.grp_description;
                        tmp.grp_isworkable = grp.grp_isworkable;
                        tmp.grp_isdeleted = grp.grp_isdeleted;

                        db.SaveChanges();
                    }

                    grp = current as Group;
                }
            }

            return grp;
        }
    }
}
